package com.msmall.community.question.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.StringUtil;
import com.msmall.community.common.Configuration;

public class QuestionUtil {

	private static final Log logger = LogFactory.getLog(QuestionUtil.class);
	
	/**
	 * Question默认Segment ID
	 * 快捷提问使用到
	 */
	
	public static Long getQuestionDefaultSegment(){
		try{
			return Long.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_DEFAULT_SEGMENT));
		}catch(Exception e){
			logger.error("===========>系统设置没有Question默认Segment ID");
			return 0L;
		}
	}
	
	/**
	 * 获文章标题最少字符数
	 * @return
	 */
	
	public static int getQuestionTitleMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_TITLE_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章标题最多字符数
	 * @return
	 */
	public static int getQuestionTitleMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_TITLE_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	/**
	 * 获得文章内容最少字符数
	 * @return
	 */
	public static int getQuestionContentMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_CONTENT_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章内容最多字符数
	 * @return
	 */
	public static int getQuestionContentMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_CONTENT_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}
	
	/**
	 * 判断文章标题字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkQuestionTitleLength(String title){
		if( StringUtil.nullOrBlank(title))
			return 2;
		int length = title.length();
		if(getQuestionTitleMinChars() > 0 ){
			if(length < getQuestionTitleMinChars())
				return 2;
		}
		if(getQuestionTitleMaxChars() > 0 ){
			if( length > getQuestionTitleMaxChars())
				return 3;
		}
		return 1;
	}
	/**
	 * 判断文章内容字符数是否在最大或最小之间
	 * @param title
	 * @return 2:字符数太少，3字符数太多，1正常
	 */
	public static int checkQuestionContentLength(String content){
		if(StringUtil.nullOrBlank(content) )
			return 2;
		int length = content.length();
		if(getQuestionContentMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getQuestionContentMinChars())
				return 2;
		}
		if(getQuestionContentMaxChars() > 0 ){
			if(length > getQuestionContentMaxChars())
				return 3;
		}
		return 1;
	}
	
	/**
	 * 获得文章回复内容最少字符数
	 * @return
	 */
	public static int getQuestionAnswerMinChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_ANSWER_MIN_CHARS));
		}catch(Exception e){
			return 1;
		}
	}
	/**
	 * 获得文章回复内容最多字符数
	 * @return
	 */
	public static int getQuestionAnswerMaxChars(){
		try{
			return Integer.valueOf(Configuration.getValue(Configuration.KLG_QUESTION_ANSWER_MAX_CHARS));
		}catch(Exception e){
			return Integer.MAX_VALUE;
		}
	}

	/**
	 * 判断文章回复内容字符数是否在最大或最小之间
	 * @param title
	 * @return  2:字符数太少，3字符数太多，1正常
	 */
	public static int checkQuestionAnswerLength(String content){
		if(StringUtil.nullOrBlank(content))
			return 2;
		int length = content.length();
		if(getQuestionAnswerMinChars() > 0 ){ // 系统设定值为负数时，不检查
			if(length < getQuestionAnswerMinChars())
				return 2;
		}
		if(getQuestionAnswerMaxChars() > 0 ){
			if(length > getQuestionAnswerMaxChars())
				return 3;
		}
		return 1;
	}
	
}
